package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.example.domain.ResponseResult;
import org.example.domain.dto.CategoryListDto;
import org.example.domain.entity.Article;
import org.example.domain.entity.Category;
import org.example.domain.vo.CategoryVo;
import org.example.domain.vo.PageVo;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.mapper.ArticleMapper;
import org.example.mapper.CategoryMapper;
import org.example.service.CategoryService;
import org.example.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    private final ArticleMapper articleMapper;

    private final CategoryMapper categoryMapper;
    @Override
    public ResponseResult getCategoryList() {

        List<Article> articles = articleMapper.getAllArticlesByStatus();

        Set<Integer> categoryIds = articles.stream()
                .map(new Function<Article, Integer>() {
                    @Override
                    public Integer apply(Article article) {
                        return article.getCategoryId();
                    }
                }).collect(Collectors.toSet());
        List<Category> categories = categoryMapper.getAllCategoryByStatus(categoryIds);

        categories = categories.stream()
                        .filter(new Predicate<Category>() {
                            @Override
                            public boolean test(Category category) {
                                return category.getStatus() == 0 && category.getDelFlag() == 0;
                            }
                        }).collect(Collectors.toList());

        System.out.println(categories);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories,CategoryVo.class);
        System.out.println(categoryVos);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult categoryList(CategoryListDto categoryListDto) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(categoryListDto.getName()), Category::getName,categoryListDto.getName());
        if(categoryListDto.getStatus() != null){
            queryWrapper.eq(Category::getStatus,categoryListDto.getStatus());
        }
        Page<Category> page = new Page<>();
        page.setCurrent(categoryListDto.getPageNum());
        page.setSize(categoryListDto.getPageSize());
        page(page,queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addCategory(Category category) {
        List<Category> categories = categoryMapper.selectByName(category);
        if(!CollectionUtils.isEmpty(categories)){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_NAME_EXIST);
        }
        return ResponseResult.okResult(save(category));
    }

    @Override
    public ResponseResult updateCategory(Category category) {
        List<Category> categories = categoryMapper.selectByName(category);
        if(categories.size()>=2){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_NAME_EXIST);
        }
        return ResponseResult.okResult(updateById(category));
    }

    @Override
    public ResponseResult categories() {
        List<Category> categories = categoryMapper.getCategories();
        return ResponseResult.okResult(categories);
    }
}
